package modelo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import modelo.Cliente;

import jdbc.ConnectionFactory;

public class ClienteDAO {
	private Connection connection;

	public ClienteDAO() {
		this.connection = new ConnectionFactory().getConnection();
	}

	public void incluir(Cliente cliente) {
		String sql = "insert into cliente (idcliente, nome, cpf, rg, logradouro, cep, telefone, sexo, email, cidade, estado, senha) "
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";

		try {
			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setInt(1, cliente.getIdCliente());
			stmt.setString(2, cliente.getNome());
			stmt.setString(3, cliente.getCpf());
			stmt.setString(4, cliente.getRg());
			stmt.setString(5, cliente.getLogradouro());
			stmt.setString(6, cliente.getCep());
			stmt.setString(7, cliente.getTelefone());
			stmt.setString(8, cliente.getSexo());
			stmt.setString(9, cliente.getEmail());
			stmt.setString(10, cliente.getCidade());
			stmt.setString(11, cliente.getEstado());
			stmt.setString(12, cliente.getSenha());

			stmt.execute();
			stmt.close();
			connection.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public List<Cliente> carrega() {

		String sql = "select * from cliente";
		try {
			List<Cliente> clientes = new ArrayList<Cliente>();
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				Cliente cliente = new Cliente();
				cliente.setIdCliente(rs.getInt("idcliente"));
				cliente.setNome(rs.getString("nome"));
				cliente.setCpf(rs.getString("cpf"));
				cliente.setRg(rs.getString("rg"));
				cliente.setLogradouro(rs.getString("logradouro"));
				cliente.setCep(rs.getString("cep"));
				cliente.setTelefone(rs.getString("telefone"));
				cliente.setSexo(rs.getString("sexo"));
				cliente.setEmail(rs.getString("email"));
				cliente.setCidade(rs.getString("cidade"));
				cliente.setEstado(rs.getString("estado"));
				cliente.setSenha(rs.getString("senha"));

				clientes.add(cliente);

			}
			rs.close();
			stmt.close();

			return clientes;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public void alterar(Cliente cliente) {
		String sql = "update from cliente set nome=?, cpf=?, rg=?, logradouro=?, cep=?, telefone=?, sexo=?, email=?, cidade=?, "
				+ "estado=?, senha=? where idcliente=?";

		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setString(1, cliente.getNome());
			stmt.setString(2, cliente.getCpf());
			stmt.setString(3, cliente.getRg());
			stmt.setString(4, cliente.getLogradouro());
			stmt.setString(5, cliente.getCep());
			stmt.setString(6, cliente.getTelefone());
			stmt.setString(7, cliente.getSexo());
			stmt.setString(8, cliente.getEmail());
			stmt.setString(9, cliente.getCidade());
			stmt.setString(10, cliente.getEstado());
			stmt.setString(11, cliente.getSenha());
			stmt.setInt(12, cliente.getIdCliente());

			stmt.execute();
			stmt.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void remover(Cliente cliente) {
		String sql = "remove from cliente where id=?";

		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setInt(1, cliente.getIdCliente());

			stmt.execute();
			stmt.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
